#region License
// Copyright (c) 2007 James Newton-King
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion

using System;
using System.Collections.Generic;
using NUnit.Framework;
using Newtonsoft.Utilities.Miscellaneous;
using Newtonsoft.Utilities.Collections;
using System.IO;
using Newtonsoft.Utilities.Testing;

namespace Newtonsoft.Utilities.Tests.Miscellaneous
{
  public enum UInt64Enum : ulong
  {
    Zero = 0,
    One = 1,
    Max = ulong.MaxValue
  }

  public class EnumUtilsTests : TestsBase
  {
    [Test]
    public void MaximumValue()
    {
      ulong maximumValue = EnumUtils.GetMaximumValue<ulong>(typeof(UInt64Enum));

      Assert.AreEqual(ulong.MaxValue, maximumValue);

      int maximumFlagsValue = EnumUtils.GetMaximumValue<int>(typeof(FileAccess));

      Assert.AreEqual(3, maximumFlagsValue);
    }

    [Test]
    public void GetNamesAndValues()
    {
      IDictionary<string, ulong> namesAndValues = EnumUtils.GetNamesAndValues<StringComparison>();

      Assert.AreEqual(namesAndValues["CurrentCulture"], 0);
      Assert.AreEqual(namesAndValues["CurrentCultureIgnoreCase"], 1);
      Assert.AreEqual(namesAndValues["InvariantCulture"], 2);
      Assert.AreEqual(namesAndValues["InvariantCultureIgnoreCase"], 3);
      Assert.AreEqual(namesAndValues["Ordinal"], 4);
      Assert.AreEqual(namesAndValues["OrdinalIgnoreCase"], 5);

      KeyedList<string, ulong> s = (KeyedList<string, ulong>)namesAndValues;

      Assert.AreEqual(s.OrderedKeys[0], "CurrentCulture");
      Assert.AreEqual(s.OrderedKeys[1], "CurrentCultureIgnoreCase");
      Assert.AreEqual(s.OrderedKeys[2], "InvariantCulture");
      Assert.AreEqual(s.OrderedKeys[3], "InvariantCultureIgnoreCase");
      Assert.AreEqual(s.OrderedKeys[4], "Ordinal");
      Assert.AreEqual(s.OrderedKeys[5], "OrdinalIgnoreCase");

      Assert.AreEqual(s.OrderedValues[0], 0);
      Assert.AreEqual(s.OrderedValues[1], 1);
      Assert.AreEqual(s.OrderedValues[2], 2);
      Assert.AreEqual(s.OrderedValues[3], 3);
      Assert.AreEqual(s.OrderedValues[4], 4);
      Assert.AreEqual(s.OrderedValues[5], 5);
    }

    [Test]
    public void UInt64Enum()
    {
      IDictionary<string, ulong> namesAndValues = EnumUtils.GetNamesAndValues<UInt64Enum, ulong>();

      Assert.AreEqual(namesAndValues["Zero"], 0);
      Assert.AreEqual(namesAndValues["One"], 1);
      Assert.AreEqual(namesAndValues["Max"], ulong.MaxValue);

      KeyedList<string, ulong> s = (KeyedList<string, ulong>)namesAndValues;

      Assert.AreEqual(s.OrderedKeys[0], "Zero");
      Assert.AreEqual(s.OrderedKeys[1], "One");
      Assert.AreEqual(s.OrderedKeys[2], "Max");

      Assert.AreEqual(s.OrderedValues[0], 0);
      Assert.AreEqual(s.OrderedValues[1], 1);
      Assert.AreEqual(s.OrderedValues[2], ulong.MaxValue);
    }

    [Test]
    [ExpectedException(typeof(Exception), ExpectedMessage = "Value from enum with the underlying type of System.UInt64 cannot be added to dictionary with a value type of System.Int32. Value was too large: 18446744073709551615")]
    public void UInt64EnumToInt()
    {
      EnumUtils.GetNamesAndValues<UInt64Enum, int>();
    }

    [Test]
    public void Parse()
    {
      StringComparison c;

      c = EnumUtils.Parse<StringComparison>("InvariantCultureIgnoreCase");
      Assert.AreEqual(StringComparison.InvariantCultureIgnoreCase, c);

      c = EnumUtils.Parse<StringComparison>("Ordinal");
      Assert.AreEqual(StringComparison.Ordinal, c);
    }

    [Test]
    [ExpectedException(typeof(ArgumentException), ExpectedMessage = @"Type System.Int32 is not an Enum.
Parameter name: T")]
    public void ParseNotEnumType()
    {
      EnumUtils.Parse<int>("InvariantCultureIgnoreCase");
    }

    [Test]
    [ExpectedException(typeof(ArgumentException))]
    public void ParseNameEmpty()
    {
      EnumUtils.Parse<StringComparison>(" ");
    }

    [Test]
    [ExpectedException(typeof(ArgumentNullException))]
    public void ParseNameNull()
    {
      EnumUtils.Parse<StringComparison>(null);
    }
  }
}